package com.renwei.yunlong.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.bean.OwnerCompanyBean;
import com.renwei.yunlong.bean.ServiceCompanyBean;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;

import java.util.List;

/**
 * 服务商 选择企业 Adapter
 *
 * Created by Administrator on 2017/9/12.
 */

public class ServiceSelectProcessingUnitAdapter extends RecyclerView.Adapter<ServiceSelectProcessingUnitAdapter.ServiceSelectProcessingUnitViewHolder>{

    private Context context;
    private LayoutInflater inflater;
    private List<OwnerCompanyBean.Rows> list;
    private OnItemClickListener mOnItemClickListener;

    public ServiceSelectProcessingUnitAdapter(Context context, List<OwnerCompanyBean.Rows> list){
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.list = list;
    }


    @Override
    public ServiceSelectProcessingUnitViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_select_company,null);

        AutoUtils.auto(view);

        return new ServiceSelectProcessingUnitViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ServiceSelectProcessingUnitViewHolder holder, int position) {


        final OwnerCompanyBean.Rows rows = list.get(position);

        holder.ivHead.setImageResource(R.mipmap.ic_launcher);
        holder.tvCompanyName.setText(rows.getServerName());
        if ("1".equals(rows.getBindFlag())) {
            holder.tvTypes.setText("好友服务商");
            holder.tvTypes.setTextColor(Color.parseColor("#3BBE7C"));
        }else if ("2".equals(rows.getBindFlag())){
            holder.tvTypes.setText("自建服务商");
            holder.tvTypes.setTextColor(Color.parseColor("#E64545"));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(holder.itemView,pos,rows.getServerName(),rows.getBindFlag(),rows.getCompanyCode());
            }
        });
    }
    @Override
    public int getItemCount() {

        return list == null?0:list.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(View view, int position, String companyName,String bindFlag,String companyCode);
    }

    static class ServiceSelectProcessingUnitViewHolder extends RecyclerView.ViewHolder{

        private View itemView;
        private ImageView ivHead;
        private TextView tvCompanyName;
        private TextView tvTypes;

        public ServiceSelectProcessingUnitViewHolder(View view) {
            super(view);
            itemView = view;
            ivHead = (ImageView) itemView.findViewById(R.id.iv_head);
            tvCompanyName = (TextView) itemView.findViewById(R.id.company_name);
            tvTypes = (TextView) itemView.findViewById(R.id.tv_type);
        }
    }
}
